<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="http://www.w3.org/1999/XSL/Format" version="1.0">

    <xsl:template name="table.cell.block.properties">
        <xsl:variable name="tabstyle"
            select="ancestor::table[1]/@tabstyle | ancestor::informaltable[1]/@tabstyle" />
        <xsl:if test="ancestor::thead">
            <xsl:attribute name="font-weight">bold</xsl:attribute>
            <xsl:attribute name="color">white</xsl:attribute>
            <xsl:attribute name="text-align">center</xsl:attribute>
            <xsl:attribute name="font-family">
                <xsl:value-of select="$title.font.family" />
            </xsl:attribute>
        </xsl:if>
    </xsl:template>

    <xsl:template name="table.cell.properties">
        <xsl:param name="bgcolor.pi" select="''" />
        <xsl:param name="rowsep.inherit" select="1" />
        <xsl:param name="colsep.inherit" select="1" />
        <xsl:param name="col" select="1" />
        <xsl:param name="valign.inherit" select="''" />
        <xsl:param name="align.inherit" select="''" />
        <xsl:param name="char.inherit" select="''" />
        <xsl:variable name="tabstyle"
            select="ancestor::table[1]/@tabstyle | ancestor::informaltable[1]/@tabstyle" />
        <xsl:variable name="bgcolor">
            <xsl:choose>
                <xsl:when test="ancestor::thead">
                    <xsl:value-of select="$title.font.color2" />
                </xsl:when>
                <xsl:when test="ancestor::thead and $tabstyle = 'invertbkg'">
                    <xsl:value-of select="$title.font.color" />
                </xsl:when>
                <xsl:when test="$bgcolor.pi != ''">
                    <xsl:value-of select="$bgcolor.pi" />
                </xsl:when>
            </xsl:choose>
        </xsl:variable>
        <xsl:if test="$bgcolor != ''">
            <xsl:attribute name="background-color">
                <xsl:value-of select="$bgcolor" />
            </xsl:attribute>
        </xsl:if>
        <xsl:choose>
            <xsl:when test="ancestor::tgroup">
                <xsl:if test="$bgcolor.pi != ''">
                    <xsl:attribute name="background-color">
                        <xsl:value-of select="$bgcolor.pi" />
                    </xsl:attribute>
                </xsl:if>
                <xsl:if test="$rowsep.inherit &gt; 0">
                    <xsl:call-template name="border">
                        <xsl:with-param name="side" select="'bottom'" />
                    </xsl:call-template>
                </xsl:if>
                <xsl:if test="$colsep.inherit &gt; 0 and $col &lt; ancestor::tgroup/@cols">
                    <xsl:call-template name="border">
                        <xsl:with-param name="side" select="'right'" />
                    </xsl:call-template>
                </xsl:if>
                <xsl:if test="$valign.inherit != ''">
                    <xsl:attribute name="display-align">
                        <xsl:choose>
                            <xsl:when test="$valign.inherit='top'">before</xsl:when>
                            <xsl:when test="$valign.inherit='middle'">center</xsl:when>
                            <xsl:when test="$valign.inherit='bottom'">after</xsl:when>
                            <xsl:otherwise>
                                <xsl:message>
                                    <xsl:text>Unexpected valign value: </xsl:text>
                                    <xsl:value-of select="$valign.inherit" />
                                    <xsl:text>, center used.</xsl:text>
                                </xsl:message>
                                <xsl:text>center</xsl:text>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:attribute>
                </xsl:if>
                <xsl:choose>
                    <xsl:when test="$align.inherit = 'char' and $char.inherit != ''">
                        <xsl:attribute name="text-align">
                            <xsl:value-of select="$char.inherit" />
                        </xsl:attribute>
                    </xsl:when>
                    <xsl:when test="$align.inherit != ''">
                        <xsl:attribute name="text-align">
                            <xsl:value-of select="$align.inherit" />
                        </xsl:attribute>
                    </xsl:when>
                </xsl:choose>
            </xsl:when>
            <xsl:otherwise>
                <!-- HTML table -->
                <xsl:variable name="border"
                    select="(ancestor::table | ancestor::informaltable)[last()]/@border" />
                <xsl:if test="$border != '' and $border != 0">
                    <xsl:attribute name="border">
                        <xsl:value-of select="$table.cell.border.thickness" />
                        <xsl:text> </xsl:text>
                        <xsl:value-of select="$table.cell.border.style" />
                        <xsl:text> </xsl:text>
                        <xsl:value-of select="$table.cell.border.color" />
                    </xsl:attribute>
                </xsl:if>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>
